package com.shinewind.payment.service.impl;

import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shinewind.payment.mapper.IWaitOrderMapper;
import com.shinewind.payment.service.IWaitOrderService;
import com.shinewind.pojo.entity.WaitOrder;
import com.shinewind.pojo.vo.CartVo;
import com.shinewind.pojo.vo.WaitOrderVo;
import net.sf.jsqlparser.statement.select.Wait;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class WaitOrderServiceImpl extends ServiceImpl<IWaitOrderMapper, WaitOrder> implements IWaitOrderService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Boolean saveWaitOrderByUidANDOrderId(String uId, String orderId) {
        if(this.lambdaQuery().eq(WaitOrder::getOrderId,orderId).eq(WaitOrder::getUId,uId).count() == 0){
            WaitOrder waitOrder = WaitOrder.builder().orderId(orderId).uId(uId).build();
            return this.save(waitOrder) == true ? true : false;
        }
        else{
            return false;
        }
    }

    @Override
    public List<String> getWaitOrderByUid(String uId) {
        //如果有未支付的订单，查询
        if(this.lambdaQuery().eq(WaitOrder::getUId,uId).count() > 0){
            List<WaitOrder> waitOrders = this.lambdaQuery().eq(WaitOrder::getUId,uId).list();
            List<String> waitOrderList = waitOrders.stream().map(waitOrder -> {
                return waitOrder.getOrderId();
            }).collect(Collectors.toList());
            return waitOrderList;
        }
        else{
            List<String> waitOrderList = new ArrayList<>();
            waitOrderList.add("没有未支付的订单");
            return waitOrderList;
        }
    }

    @Override
    public List<WaitOrderVo> getRelateInfoByRedis(List<String> waitOrderList) {
        //根据未支付的订单列表从redis中查询关联信息
        List<WaitOrderVo> waitOrderVos = waitOrderList.stream().map(key ->{
            WaitOrderVo waitOrderVo = WaitOrderVo.builder().build();
            //按照当前的订单id从redis中读取商品列表信息
            List<CartVo> goodsList = redisTemplate.opsForList().range(key,0,-1);
            waitOrderVo.setGoodsList(goodsList);
            //获取订单创建时间
            waitOrderVo.setCreateTime(this.lambdaQuery().eq(WaitOrder::getOrderId,key).one().getCreateTime());
            //获取订单号
            waitOrderVo.setOrderId(key);
            return waitOrderVo;
        }).collect(Collectors.toList());
        return waitOrderVos;
    }

    @Override
    public Long isExistInWaitOrder(String orderId) {
        return this.lambdaQuery().eq(WaitOrder::getOrderId,orderId).count();
    }

    @Override
    public Boolean deleteWaitOrderByOrderId(String out_trade_no) {
        return this.lambdaUpdate().eq(WaitOrder::getOrderId,out_trade_no).remove();
    }

    @Override
    public Boolean cancelWaitOrderByOrderId(String orderId) {
        //从待支付表中查询订单，如果有就删除
        if(this.lambdaQuery().eq(WaitOrder::getOrderId,orderId).count() > 0){
            //删除
            return this.lambdaUpdate().eq(WaitOrder::getOrderId,orderId).remove();
        }
        else{
            return false;
        }
    }
}
